/*
 * The application needs to have the permission to write to external storage
 * if the output file is written to the external storage, and also the
 * permission to record audio. These permissions must be set in the
 * application's AndroidManifest.xml file, with something like:
 *
 * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 * <uses-permission android:name="android.permission.RECORD_AUDIO" />
 *
 */
package co.mobiless.noterecorder;

import android.os.Environment;
import android.content.Context;
import android.util.Log;
import android.media.MediaRecorder;
import android.media.MediaPlayer;

import java.io.IOException;


public class AudioRecordTest //implements OnCompletionListener//extends AsyncTask<String, Void, Void>
{
    private static final String LOG_TAG = "AudioRecordTest";
    private static String mFileName = null;
    private Context _context;
    private MediaRecorder mRecorder = null;

    public MediaPlayer   mPlayer = null;


    private void onRecord(boolean start) {	
        if (start) {
            //startRecording();
        } else {
            stopRecording();
        }
    }

    private void onPlay(boolean start) {
        if (start) {
            //startPlaying();
        } else {
            stopPlaying();
        }
    }

    public void startPlaying(String srtFileName) {
        try {
        	if(mPlayer == null){
        		mPlayer = new MediaPlayer();
        	}
            mPlayer.setDataSource(srtFileName);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }

    public void stopPlaying() {
        mPlayer.release();
        mPlayer = null;
    }
    public void pausePlaying() {
        mPlayer.pause();
    }
    public void resumePlaying() {
        mPlayer.start();
    }
    public void goToMsec(long mSec)
    {
    	mPlayer.seekTo((int)(mSec));
    }
    public void startRecording(String srtFileName) {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.setOutputFile(srtFileName);
        

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start();
    }

    public void stopRecording() {
    	mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }

    
    public AudioRecordTest(Context context) {
       _context = context;
    	mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName += "/audiorecordtest.3gp";
    }

	/*public void onCompletion(MediaPlayer arg0) {
		Log.d("PlayBack","Fin reproduccion");
		Toast.makeText(_context, "fin resproduccion" , Toast.LENGTH_LONG).show();
	}
*/
	/*@Override
	protected Void doInBackground(String... params) {
		startRecording();
		return null;
	}
	 protected void onCancelled(String... params){
		 stopRecording();
	 }*/

       
/*    @Override
    public void onPause() {
        super.onPause();
        if (mRecorder != null) {
            mRecorder.release();
            mRecorder = null;
        }

        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }*/
}